//
//  LoginSMSView.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/4/12.
//

import SwiftUI
import FlowStacks

struct LoginSMSView: View {
    
    @EnvironmentObject var navigator: FlowNavigator<LoginStack>
    @StateObject var viewModel = LoginSMSViewModel()
    
    let leftOffset = 24.0
    var body: some View {
        VStack{
            HStack{
                NavigationBar(title: "")
            }
            HStack(alignment: .center, content: {
                Spacer()
                HeaderView()
                Spacer()
            }).padding(.top, 44)
            HStack{
                CustomTextField(
                    text: $viewModel.account,
                    placeholder: "email or phoneNum",
                    isValid: viewModel.account.isEmpty || viewModel.account.isEmail || viewModel.account.isPhoneNum
                )
                .disabled(viewModel.isLoading)
            }.padding(.horizontal, leftOffset)
                .padding(.top, 40)
            
            HStack {
                AgreeView(isAgree: $viewModel.isAgreed)
                    .padding(.leading, leftOffset)
                Spacer()
            }.padding(.top, 15)
            
            HStack {
                Button(action: {
                    viewModel.requestCode()
                }) {
                    ZStack {
                        if viewModel.isLoading {
                            ProgressView()
                                .progressViewStyle(CircularProgressViewStyle(tint: .white))
                        } else {
                            Text("login")
                                .font(.system(size: 16, weight: .medium))
                        }
                    }
                    .frame(maxWidth: .infinity)
                    .frame(height: 44)
                    .background(AppColors.Brand6)
                    .cornerRadius(6)
                    .foregroundColor(.white)
                    .contentShape(Rectangle())
                }
                .padding(.horizontal, leftOffset)
            }.padding(.top, 25)
            // 密码登录
            HStack {
                Spacer()
                Button("account password login") {
                    navigator.pop()
                }.foregroundColor(AppColors.Gy2)
                    .font(.system(size: 14))
                    .padding(.trailing, leftOffset)
            }
            .padding(.top, 15)
            Spacer()
            HStack {
                Rectangle()
                    .frame(maxWidth: .infinity)
                    .frame(height: 1)
                    .foregroundColor(AppColors.Gray2)
                Text("other login type")
                    .font(.system(size: 14))
                    .foregroundColor(AppColors.Gy3)
                Rectangle()
                    .frame(maxWidth: .infinity)
                    .frame(height: 1)
                    .foregroundColor(AppColors.Gray2)
            }.padding(.horizontal, 24)
                .padding(.bottom, 25)
            HStack {
                Spacer()
                Button(action: {
                    viewModel.wechatLogin()
                }, label: {
                    Image("login_wechat")
                })
                Spacer()
            }.padding(.horizontal, 24)
                .padding(.bottom, 48)
        }.onChange(of: viewModel.sendSuccess) { result in
            if result {
                navigator.push(.verificationCode(account: viewModel.account, type: .notRegister, verifyType: .loginOrBinding))
            }
        }
        .onAppear{
            viewModel.resetStatus()
        }
        .toast(isPresented:$viewModel.showToast , message: viewModel.toastMsg)
    }
}
